package org.tungse.controller;


import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphO;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tungse.domain.User;
import org.tungse.service.UserService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Author : Tungse
 * @Date: 2025/7/2 10:34
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/list")
    public List<User> getUserList() {
        return userService.getList();
    }


    //资源名称
    public static final String RESOURCE_NAME = "userList";

    /**
     * 限流方式的访问   抛出异常的方式
     */
    @RequestMapping("/list1")
    public List<User> getUserList1() {
        List<User> userList = null;
        Entry entry = null;
        try {
            // 被保护的业务逻辑
            entry = SphU.entry(RESOURCE_NAME);
            userList = userService.getList();
        } catch (BlockException e) {
            // 资源访问阻止，被限流或被降级
            return Collections.singletonList(new User("xxx", "资源访问被限流", 0));
        } catch (Exception e) {
            // 若需要配置降级规则，需要通过这种方式记录业务异常
            Tracer.traceEntry(e, entry);
        } finally {
            // 务必保证 exit，务必保证每个 entry 与 exit 配对
            if (entry != null) {
                entry.exit();
            }
        }
        return userList;
    }



    public static final String RESOURCE_NAME_QUERY_USER_BY_ID = "queryUserById";
    /**
     * 限流方式的访问   返回布尔值的方式
     */
    @RequestMapping("/get/{id}")
    public String queryUserById(@PathVariable("id") String id) {
        if (SphO.entry(RESOURCE_NAME_QUERY_USER_BY_ID)) {
            try {
                //被保护的逻辑
                //模拟数据库查询数据
                return JSONObject.toJSONString(new User(id, "Tom", 25));
            } finally {
                //关闭资源
                SphO.exit();
            }
        } else {
            //资源访问阻止，被限流或被降级
            return "Resource is Block!!!";
        }
    }

    /**
     * 限流方式的访问   service层注解的方式
     */
    @RequestMapping("/getUser")
//    @SentinelResource(value = "AAAAAAAAAAAA", blockHandler = "queryUserByUserNameBlock1")
    public String queryUserByName(String name) {
        //被保护的逻辑
        //模拟数据库查询数据
        return JSONObject.toJSONString(userService.queryByUserName(name));
//        return JSONObject.toJSONString(new User("assss", name, 25));
    }
    public User queryUserByUserNameBlock1(String name, BlockException ex) {
        //打印异常
        ex.printStackTrace();
        return new User("xxx", "用户名称：{" + name + "},资源访问被限流", 0);
    }
}

